<template>
	<div class="wrapper">
		<!-- header部分 -->
		<header>
			<div class="icon-location-box">
				<div class="icon-location"></div>
			</div>
			<div class="location-text">天津大学北洋园校区<i class="fa fa-caret-down"></i></div>
		</header>
	
		<!-- search部分 -->
		<!-- 
				    搜索框部分（此块与search-fixed-top块宽度高度一致，用于当
					search-fixed-top块固定后，挡住下面块不要窜上去） 
				-->
		<div class="search">
			<!-- 当滚动条超过上面的定位块时，search-fixed-top块变成固定在顶部。 -->
			<div class="search-fixed-top" ref="fixedBox">
				<!-- 搜索框部分中间的白框 -->
				<div class="search-box">
					<i class="fa fa-search"></i>
					<input class="search-box" type="text" v-model="searching" placeholder="搜索饿了么商家名称">
					<button @click="search()">搜索</button>
				</div>
			</div>
		</div>
	
		<!-- 点餐分类部分 -->
		<ul class="foodtype">
			<li @click="toBusinessList(1)">
				<img src="../assets/dcfl01.png">
				<p>美食</p>
			</li>
			<li @click="toBusinessList(2)">
				<img src="../assets/dcfl02.png">
				<p>早餐</p>
			</li>
			<li @click="toBusinessList(3)">
				<img src="../assets/dcfl03.png">
				<p>跑腿代购</p>
			</li>
			<li @click="toBusinessList(4)">
				<img src="../assets/dcfl04.png">
				<p>汉堡披萨</p>
			</li>
			<li @click="toBusinessList(5)">
				<img src="../assets/dcfl05.png">
				<p>甜品饮品</p>
			</li>
			<li @click="toBusinessList(6)">
				<img src="../assets/dcfl06.png">
				<p>速食简餐</p>
			</li>
			<li @click="toBusinessList(7)">
				<img src="../assets/dcfl07.png">
				<p>地方小吃</p>
			</li>
			<li @click="toBusinessList(8)">
				<img src="../assets/dcfl08.png">
				<p>米粉面馆</p>
			</li>
			<li @click="toBusinessList(9)">
				<img src="../assets/dcfl09.png">
				<p>包子粥铺</p>
			</li>
			<li @click="toBusinessList(10)">
				<img src="../assets/dcfl10.png">
				<p>炸鸡炸串</p>
			</li>
		</ul>
	
		<!-- 横幅广告部分（注意：此处有背景图片） -->
		<div class="banner">
			<h3>品质套餐</h3>
			<p>搭配齐全吃得好</p>
			<a>立即抢购 &gt;</a>
		</div>
	
		<!-- 超级会员部分 -->
		<div class="supermember">
			<div class="left">
				<img src="../assets/super_member.png">
				<h3>超级会员</h3>
				<p>&#8226; 每月享超值权益</p>
			</div>
			<div class="right">
				立即开通 &gt;
			</div>
		</div>
	
		<!-- 推荐商家部分 -->
		<div class="recommend">
			<div class="recommend-line"></div>
			<p>推荐商家</p>
			<div class="recommend-line"></div>
		</div>
	
		<!-- 推荐方式部分 -->
		<ul class="recommendtype">
			<li>销量最高<i class="fa fa-caret-down"></i></li>
			<li @click="update()">筛选<i class="fa fa-filter"></i></li>
		</ul>
		<div class="conditions">
			<input type="text" v-model="starPrice" placeholder="请输入最高起送费">
			<input type="text" v-model="deliveryPrice" placeholder="请输入最高配送费">
		</div>
		
	    <ul class="business" v-if="businessList.length">
			<li v-for="item in businessList" @click="toBusinessInfo(item.businessId)">
				<div class="business-img">
					<img :src="item.businessImg">
				</div>
				<div class="business-info">
					<h3>{{item.businessName}}</h3>
					<p>&#165;{{item.starPrice}}起送 | &#165;{{item.deliveryPrice}}配送 | {{item.sales}}销量</p>
					<p>{{item.businessExplain}}</p>
				</div>
			</li>
		</ul>
		<p v-else>没有商家数据。</p>
		<p>------------------------------------------------------</p>
	<!--底部菜单部分-->
	<Footer></Footer>
	</div>
</template>
        
<script>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import Footer from '../components/Footer.vue';
import axios from 'axios';
import qs from 'qs';
import router from '../router';

export default {
  name: 'SaleIndex',
  setup() {
    const businessList = ref([]);
    const starPrice = ref('');
    const deliveryPrice = ref('');
    const searching = ref('');
    const postingStarPrice = ref('');
    const postingDeliveryPrice = ref('');

    const fixedBox = ref(null);

    onMounted(() => {
      const getBusinessList = async () => {
        try {
          const response = await axios.post('BusinessController/getBusinessOrderBySalesDesc', qs.stringify({}));
          businessList.value = response.data;
        } catch (error) {
          console.error(error);
        }
      };
      getBusinessList();

      document.onscroll = () => {
        const scroll = document.documentElement.scrollTop || document.body.scrollTop;
        const width = document.documentElement.clientWidth;
        if (scroll > width * 0.12) {
          fixedBox.value.style.position = 'fixed';
          fixedBox.value.style.left = '0';
          fixedBox.value.style.top = '0';
        } else {
          fixedBox.value.style.position = 'static';
        }
      };
    });

    onBeforeUnmount(() => {
      document.onscroll = null;
    });

    const toBusinessList = (orderTypeId) => {
      router.push({ path: '/businessList', query: { orderTypeId } });
    };

    const toBusinessInfo = (businessId) => {
      router.push({ path: '/businessInfo', query: { businessId } });
    };

    const update = async () => {
      postingStarPrice.value = starPrice.value || 2147483647;
      postingDeliveryPrice.value = deliveryPrice.value || 2147483647;
      try {
        const response = await axios.post('BusinessController/listBusinessByConditions', qs.stringify({
          starPrice: postingStarPrice.value,
          deliveryPrice: postingDeliveryPrice.value
        }));
        businessList.value = response.data;
      } catch (error) {
        console.error(error);
      }
    };

    const search = async () => {
      try {
        const response = await axios.post('BusinessController/searching', qs.stringify({
          businessName: searching.value
        }));
        businessList.value = response.data;
      } catch (error) {
        console.error(error);
      }
    };

    return {
      businessList,
      starPrice,
      deliveryPrice,
      searching,
      toBusinessList,
      toBusinessInfo,
      update,
      search,
      fixedBox
    };
  },
  components: {
    Footer
  }
};
</script>


<style scoped>
	.wrapper {
		width: 100%;
		height: 100%;
	}
	
	/****************** header ******************/
	.wrapper header {
		width: 100%;
		height: 12vw;
		background-color: #0097FF;
	
		display: flex;
		align-items: center;
	}
	
	.wrapper header .icon-location-box {
		width: 3.5vw;
		height: 3.5vw;
		margin: 0 1vw 0 3vw;
	}
	
	.wrapper header .location-text {
		font-size: 4.5vw;
		font-weight: 700;
		color: #fff;
	}
	
	.wrapper header .location-text .fa-caret-down {
		margin-left: 1vw;
	}
	
	/****************** search ******************/
	.wrapper .search {
		margin-top: 12vw;
		width: 100%;
		height: 13vw;
	}
	
	.wrapper .search .search-fixed-top {
		width: 100%;
		height: 13vw;
		background-color: #0097FF;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.wrapper .search .search-fixed-top .search-box {
		width: 90%;
		height: 9vw;
		background-color: #fff;
		border-radius: 2px;
	
		display: flex;
		justify-content: center;
		align-items: center;
	
		font-size: 3.5vw;
		color: #AEAEAE;
		font-family: "宋体";
		/*此样式是让文本选中状态无效*/
		user-select: none;
	}
	
	.wrapper .search .search-fixed-top .search-box .fa-search {
		margin-right: 1vw;
	}
	
	/****************** 点餐分类部分 ******************/
	.wrapper .foodtype {
		width: 100%;
		height: 48vw;
	
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
		/*要使用align-content。10个子元素将自动换行为两行，而且两行作为一个整体垂直居中*/
		align-content: center;
	}
	
	.wrapper .foodtype li {
		/*一共10个子元素，通过计算，子元素宽度在16.7 ~ 20 之间，才能保证换两行*/
		width: 18vw;
		height: 20vw;
	
		display: flex;
		/*弹性盒子主轴方向设为column，然后仍然是垂直水平方向居中*/
		flex-direction: column;
		justify-content: center;
		align-items: center;
	
		user-select: none;
		cursor: pointer;
	}
	
	.wrapper .foodtype li img {
		width: 12vw;
		/*视频讲解时高度设置为12vw，实际上设置为10.3vw更佳*/
		height: 10.3vw;
	}
	
	.wrapper .foodtype li p {
		font-size: 3.2vw;
		color: #666;
	}
	
	/****************** 横幅广告部分 ******************/
	.wrapper .banner {
		/**
		 * 设置容器宽度95%，然后水平居中，这样两边留白; 
		 * 这里不能用padding，因为背景图片也会覆盖padding
		 */
		width: 95%;
		margin: 0 auto;
		height: 29vw;
	
		/*此三个样式组合，可以保证背景图片充满整个容器*/
		background-image: url(../assets/index_banner.png);
		background-repeat: no-repeat;
		background-size: cover;
	
		box-sizing: border-box;
		padding: 2vw 6vw;
	}
	
	.wrapper .banner h3 {
		font-size: 4.2vw;
		margin-bottom: 1.2vw;
	}
	
	.wrapper .banner p {
		font-size: 3.4vw;
		color: #666;
		margin-bottom: 2.4vw;
	}
	
	
	.wrapper .banner a {
		font-size: 3vw;
		color: #C79060;
		font-weight: 700;
	}
	
	/****************** 超级会员部分 ******************/
	.wrapper .supermember {
		/*这里也设置容器宽度95%，不能用padding，因为背景色也会充满padding*/
		width: 95%;
		margin: 0 auto;
		height: 11.5vw;
		background-color: #FEEDC1;
		margin-top: 1.3vw;
		border-radius: 2px;
		color: #644F1B;
	
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.wrapper .supermember .left {
		display: flex;
		align-items: center;
		margin-left: 4vw;
		user-select: none;
	}
	
	.wrapper .supermember .left img {
		width: 6vw;
		height: 6vw;
		margin-right: 2vw;
	}
	
	.wrapper .supermember .left h3 {
		font-size: 4vw;
		margin-right: 2vw;
	}
	
	.wrapper .supermember .left p {
		font-size: 3vw;
	}
	
	.wrapper .supermember .right {
		font-size: 3vw;
		margin-right: 4vw;
		cursor: pointer;
	}
	
	/****************** 推荐商家部分 ******************/
	.wrapper .recommend {
		width: 100%;
		height: 14vw;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	
	.wrapper .recommend .recommend-line {
		width: 6vw;
		height: 0.2vw;
		background-color: #888;
	}
	
	.wrapper .recommend p {
		font-size: 4vw;
		margin: 0 4vw;
	}
	
	/****************** 推荐方式部分 ******************/
	.wrapper .recommendtype {
		width: 100%;
		height: 5vw;
		margin-bottom: 5vw;
	
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	
	.wrapper .recommendtype li {
		font-size: 3.5vw;
		color: #555;
	}
	
	.wrapper .conditions{
	    display: flex;
	    justify-content: center; /* 水平居中 */
	    align-items: center; /* 垂直居中（如果需要） */
	    gap: 20px; /* 两个输入框之间的间距 */
	  }
	
	/****************** 商家列表部分 ******************/
	.wrapper .business {
		width: 100%;
		margin-top: 12vw;
		margin-bottom: 14vw;
	}
	
	.wrapper .business li {
		width: 100%;
		box-sizing: border-box;
		padding: 2.5vw;
		border-bottom: solid 1px #DDD;
		user-select: none;
		cursor: pointer;
	
		display: flex;
		align-items: center;
	}
	
	.wrapper .business li .business-img {
		/*这里设置为相当定位，成为business-img-quantity元素的父元素*/
		position: relative;
	}
	
	.wrapper .business li .business-img img {
		width: 20vw;
		height: 20vw;
	}
	
	.wrapper .business li .business-img .business-img-quantity {
		width: 5vw;
		height: 5vw;
		background-color: red;
		color: #fff;
		font-size: 3.6vw;
		border-radius: 2.5vw;
	
		display: flex;
		justify-content: center;
		align-items: center;
	
		/*设置成绝对定位，不占文档流空间*/
		position: absolute;
		right: -1.5vw;
		top: -1.5vw;
	}
	
	.wrapper .business li .business-info {
		margin-left: 3vw;
	}
	
	.wrapper .business li .business-info h3 {
		font-size: 3.8vw;
		color: #555;
	}
	
	.wrapper .business li .business-info p {
		font-size: 3vw;
		color: #888;
		margin-top: 2vw;
	}
</style>
